/* GameWin.java
 * Created by Jordan Klassen on 23-May-2006 at 6:58:47 PM
 */

package mduel;


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class GameWin extends Canvas implements Runnable, WindowListener, KeyListener{
private static final long serialVersionUID = 1L;

public JFrame window = null;

public SpriteBank bank = null;

public BufferStrategy bufferDouble = null;

public Thread runner = null;

public Player players = null;

public long usedTime;

public GameWin()
    {
	super();
    setSize(640, 480);
    window = new JFrame("MDuel");
    window.setBounds(0, 0, 640, 480);
    window.add(this);
    window.addKeyListener(this);
    this.addKeyListener(this);
    window.setVisible(true);
    
    bank = new SpriteBank();
    String[] src_array = {"menu.gif","images/stand.gif","images/run1.gif","images/run2.gif","images/run3.gif","images/run4.gif",
                          "images/jump7.gif","images/jumpend.gif","images/roll1.gif","images/roll2.gif","images/roll3.gif","images/roll4.gif",};
    String[] dest_name = {"dudes",   "stand",           "run1",           "run2",           "run3",           "run4",
                          "jump",            "halfcrouch"        ,"roll1",           "roll2",           "roll3",           "roll4",           };
    bank.loadAllImages(src_array, dest_name);
    
    createBufferStrategy(2);
    bufferDouble = getBufferStrategy();
    players = new Player(10,40, bank);
    
    runner = new Thread(this);
    runner.start();
    this.setBackground(Color.black);
    }
public void paint_win()
    {
    Graphics g = bufferDouble.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0,0,640,480);

    players.drawSelf(g, this);
    //g.drawImage(bank.retrieveImage("dudes"), 1, 1, this);
    g.setColor(Color.white);
    if (usedTime > 0)
        { g.drawString(String.valueOf(1000/usedTime)+" fps",0,200);}
        //System.out.println(String.valueOf(1000/usedTime)+" fps");}
    else
        {g.drawString("--- fps",0,200);}
        //System.out.println("--- fps");}*/
    bufferDouble.show();
    }

public void run()
    {
    usedTime=1000;
    while (runner != null)
        {
        long startTime = System.currentTimeMillis();
        players.updateSelf();
        paint_win();
        usedTime = System.currentTimeMillis()-startTime;
        try{Thread.sleep(50);}
        catch(Exception e)
        {}
        }
    }
public JFrame getWin()
    {
    return window;
    }

public void windowOpened(WindowEvent arg0)
    {}

public void windowClosing(WindowEvent e)
    {runner = null;
    
    }

public void windowClosed(WindowEvent e)
    {}

public void windowIconified(WindowEvent e)
    {}

public void windowDeiconified(WindowEvent e)
    {}

public void windowActivated(WindowEvent e)
    {}

public void windowDeactivated(WindowEvent e)
    {}

public void keyTyped(KeyEvent arg0) {}

public void keyPressed(KeyEvent ke) {
	players.setAllPKstate(ke,true);
	//System.out.println("blah");
}

public void keyReleased(KeyEvent ke) {
	players.setAllPKstate(ke, false);
	
}


}
